pandas increment value on condition

36

In [47]: df['ans'] = (df['ix'] != df['ix'].shift(1)).cumsum()

In [48]: df
Out[48]: 
   index  ix  ans
0      1  pa    1
1      2  pa    1
2      3  pa    1
3      4  pe    2
4      5  fc    3
5      6  pb    4
6      7  pb    4
7      8  df    5

Comments

Submit
0 Comments